//
//  WeatherViewController.m
//  YuTang
//
//  Created by 葛悦彣 on 2024/9/26.
//

#import "WeatherViewController.h"
#import "WeatherModel.h"

@interface WeatherViewController ()



@end

@implementation WeatherViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = KcolorHex(@"#D0E2F3");
    UIImageView * backimg = kimageView(@"weather_banner");
    [self.view addSubview:backimg];
    [backimg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.offset(0);
        make.height.offset(kwt(450));
    }];
    MJWeakSelf
    UIView * topview = [UIView new];
    [self.view addSubview:topview];
    [topview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.offset(0);
        make.height.offset(Knav_HeightX);
    }];
    
    UIView * backview = [UIView new].addClickBlock(^{
        [weakSelf back];
    });
    [topview addSubview:backview];
    [backview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.offset(44);
        make.left.bottom.right.offset(0);
        make.width.offset(kwt(120));
    }];
    
    UIImageView * leftimg = kimageView(@"white_path");
    [backview addSubview:leftimg];
    [leftimg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(20);
        make.left.offset(kwt(10));
        make.centerY.offset(0);
    }];
    
    
    WeatherModel * model = [WeatherModel shared];
    
    if(model.daily.count < 2 || model.hourly.count < 1){
        [self.navigationController popViewControllerAnimated:YES];
        [[DTKTool getCurrentVC].view showWarning:@"未获取到数据,请稍后重试"];
    }
    UILabel * leftlab = [DTKTool labelWithText:model.address andFont:kCFont(19) andTextColor:UIColor.whiteColor];
    [backview addSubview:leftlab];
    [leftlab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(43));
        make.right.offset(kwt(-20));
        make.centerY.offset(0);
    }];
    
    UIScrollView * mainScrollview = [[UIScrollView alloc]initWithFrame:CGRectMake(0, Knav_HeightX, KSCREEN_WIDTH, KSCREEN_HEIGHT - Knav_HeightX)];
    mainScrollview.contentInset = UIEdgeInsetsZero;
    if (@available(iOS 11.0, *)) {
        mainScrollview.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    if (@available(iOS 13.0, *)) {
        mainScrollview.automaticallyAdjustsScrollIndicatorInsets = YES;
    }
    //    self.automaticallyAdjustsScrollViewInsets = NO;
    
    mainScrollview.contentSize = CGSizeMake(0, kwt(1050));
    mainScrollview.bounces = NO;
    [self.view addSubview:mainScrollview];
    
    UILabel * temp1 = [DTKTool labelWithText:[NSString stringWithFormat:@"%@°" ,model.hourlyModel.temp] andFont:kCFont(90) andTextColor:UIColor.whiteColor];
    [mainScrollview addSubview:temp1];
    [temp1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(kwt(50));
        make.centerX.offset(0);
        make.height.offset(kwt(90));
    }];
    
    UILabel * temp2 = [DTKTool labelWithText:model.hourlyModel.text andFont:kCFont(24) andTextColor:UIColor.whiteColor];
    [mainScrollview addSubview:temp2];
    [temp2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(kwt(150));
        make.centerX.offset(0);
        make.height.offset(kwt(24));
        
    }];
    
    UILabel * temp3 = [DTKTool labelWithText:[NSString stringWithFormat:@"最高%@°  最低%@°",model.dailyModel.tempMax,model.dailyModel.tempMin] andFont:kCFont(24) andTextColor:UIColor.whiteColor];
    [mainScrollview addSubview:temp3];
    [temp3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(kwt(190));
        make.centerX.offset(0);
    }];
    
    
    UIView * tweDayView = [UIView new];
    tweDayView.backgroundColor = UIColor.whiteColor;
    [DTKTool YJ:kwt(8) andView:tweDayView];
    [mainScrollview addSubview:tweDayView];
    [tweDayView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(kwt(250));
        make.width.offset(kwt(360));
        make.height.offset(kwt(90));
        make.centerX.offset(0);
    }];
    
    UIView * line = [UIView new];
    line.backgroundColor = KcolorHex(@"#E5E5E5");
    [tweDayView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.offset(0);
        make.height.offset(kwt(62));
        make.width.offset(0.5);
    }];
    
    UILabel * jtwdlab =  [DTKTool labelWithText:[NSString stringWithFormat:@"%@°~%@°",model.dailyModel.tempMin,model.dailyModel.tempMax] andFont:kCFont(22) andTextColor:KcolorHex(@"#333333")];
    [tweDayView addSubview:jtwdlab];
    [jtwdlab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(kwt(20));
        make.left.offset(kwt(13));
    }];
    
    
    UILabel * jtlab = [DTKTool labelWithText:@"今天" andFont:kFont(16) andTextColor:KcolorHex(@"#545456")];
    [tweDayView addSubview:jtlab];
    [jtlab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(13));
        make.bottom.offset(kwt(-14));
    }];
    
    UIImageView * jtimg = kimageView(model.dailyModel.iconDay);
    jtimg.contentMode = UIViewContentModeScaleAspectFit;

    [tweDayView addSubview:jtimg];
    [jtimg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(kwt(20));
        make.right.equalTo(line.mas_left).offset(kwt(-18));
        make.width.height.offset(kwt(34));
    }];
    
    
    UILabel * jttqlab = [DTKTool labelWithText:model.dailyModel.textDay andFont:kCFont(16) andTextColor:KcolorHex(@"#333333")];
    [tweDayView addSubview:jttqlab];
    [jttqlab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(jtimg.mas_right);
        make.bottom.offset(kwt(-14));
    }];
    
    UILabel * mtwdlab =  [DTKTool labelWithText:[NSString stringWithFormat:@"%@°~%@°",model.daily[1].tempMin,model.daily[1].tempMax] andFont:kCFont(22) andTextColor:KcolorHex(@"#333333")];
    [tweDayView addSubview:mtwdlab];
    [mtwdlab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(kwt(20));
        make.left.equalTo(line.mas_right).offset(kwt(13));
    }];
    
    
    UILabel * mtlab = [DTKTool labelWithText:@"明天" andFont:kFont(16) andTextColor:KcolorHex(@"#545456")];
    [tweDayView addSubview:mtlab];
    [mtlab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(line.mas_right).offset(kwt(13));
        make.bottom.offset(kwt(-14));
    }];
    
    UIImageView * mtimg = kimageView(model.daily[1].iconDay);
    mtimg.contentMode = UIViewContentModeScaleAspectFit;

    [tweDayView addSubview:mtimg];
    [mtimg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(kwt(20));
        make.right.offset(kwt(-18));
        make.width.height.offset(kwt(34));
    }];
    
    UILabel * mttqlab = [DTKTool labelWithText:model.daily[1].textDay andFont:kCFont(16) andTextColor:KcolorHex(@"#333333")];
    [tweDayView addSubview:mttqlab];
    [mttqlab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(mtimg.mas_right);
        make.bottom.offset(kwt(-14));
    }];
    
    
    UIView * threeDayView = [UIView new];
    threeDayView.backgroundColor = UIColor.whiteColor;
    [DTKTool YJ:kwt(8) andView:threeDayView];
    [mainScrollview addSubview:threeDayView];
    [threeDayView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(tweDayView.mas_bottom).offset(kwt(10));
        make.width.offset(kwt(360));
        make.height.offset(kwt(200));
        make.centerX.offset(0);
    }];
    
    UILabel * temp4 = [DTKTool labelWithText:@"24小时预报" andFont:kCFont(20) andTextColor:KcolorHex(@"#333333")];
    [threeDayView addSubview:temp4];
    [temp4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(kwt(15));
        make.left.offset(kwt(13));
    }];
    

    UIScrollView * hourview = [UIScrollView new];
    [threeDayView addSubview:hourview];
    hourview.frame = CGRectMake(0, kwt(45), kwt(360), kwt(160));
    
    CGFloat X = kwt(13);
    for (WeatherHearModel * hearmodel in model.hourly) {
        UIView * hearview = [UIView new];
        [hourview addSubview:hearview];
        hearview.frame = CGRectMake(X, 0, kwt(70), kwt(140));
        [DTKTool YJBK:kwt(7) andView:hearview andWidth:1 andColor:KcolorHex(@"#F2F2F2")];
//        [DTKTool YJYY:kwt(7) andView:hearview andWidth:1 andColor:KcolorHex(@"#84C7FF") andSize:CGSizeMake(0, 0) andOpacity:0.5];
        NSString * time = [NSString stringWithFormat:@"%@时", [[hearmodel.fxTime componentsSeparatedByString:@"T"].lastObject componentsSeparatedByString:@":"].firstObject];
        if([[hearmodel.fxTime componentsSeparatedByString:@"T"].firstObject isEqualToString:[NSDate br_stringFromDate:[NSDate new] dateFormat:@"yyyy-MM-dd"]]){
            if([NSDate new].br_hour == [[[hearmodel.fxTime componentsSeparatedByString:@"T"].lastObject componentsSeparatedByString:@":"].firstObject intValue]){
                time = @"现在";
                hearview.backgroundColor = KcolorHex(@"#E2EAF7");
                [DTKTool YJBK:kwt(7) andView:hearview andWidth:1 andColor:KcolorHex(@"#D4DCDF")];
                [DTKTool YJYY:kwt(7) andView:hearview andWidth:1 andColor:KcolorHex(@"#84C7FF") andSize:CGSizeMake(0, 0) andOpacity:0.5];
            }
        }
        UILabel * temp31 = [DTKTool labelWithText:time andFont:kCFont(17) andTextColor:KcolorHex(@"#222222")];
        [hearview addSubview:temp31];
        [temp31 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.offset(0);
            make.top.offset(kwt(15));
            make.height.offset(kwt(17));
        }];
        UILabel * temp32 = [DTKTool labelWithText:hearmodel.text andFont:kFont(15) andTextColor:KcolorHex(@"#787878")];
        [hearview addSubview:temp32];
        [temp32 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.offset(0);
            make.top.offset(kwt(44));
        }];
        UIImageView * tempimg = kimageView(hearmodel.icon);
        tempimg.contentMode = UIViewContentModeScaleAspectFit;

        [hearview addSubview:tempimg];
        [tempimg mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.offset(kwt(66));
            make.centerX.offset(0);
            make.width.height.offset(kwt(34));        }];
        
        UILabel * temp33 = [DTKTool labelWithText:[NSString stringWithFormat:@"%@°",hearmodel.temp] andFont:kCFont(18) andTextColor:KcolorHex(@"#222222")];
        [hearview addSubview:temp33];
        [temp33 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.offset(0);
            make.top.offset(kwt(107));
            make.height.offset(kwt(17));
        }];
        
        X += kwt(77);
    }
    hourview.contentSize = CGSizeMake(X, 0);
    
    UIView * fourDayView = [UIView new];
    fourDayView.backgroundColor = UIColor.whiteColor;
    [DTKTool YJ:kwt(8) andView:fourDayView];
    [mainScrollview addSubview:fourDayView];
    [fourDayView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(threeDayView.mas_bottom).offset(kwt(10));
        make.width.offset(kwt(360));
        make.height.offset(kwt(56 * 8 - 8));
        make.centerX.offset(0);
    }];
    
    UILabel * temp5 = [DTKTool labelWithText:@"未来7天预报" andFont:kCFont(20) andTextColor:KcolorHex(@"#333333")];
    [fourDayView addSubview:temp5];
    [temp5 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(kwt(15));
        make.left.offset(kwt(13));
    }];
    
    
    CGFloat Y = kwt(48);
    int min = 100;
    int max = 0;
    for (WeatherDayModel *daymodel in model.daily) {
        if(daymodel.tempMin.intValue < min){
            min = daymodel.tempMin.intValue;
        }
        if(daymodel.tempMax.intValue > max){
            max = daymodel.tempMax.intValue;
        }
    }
    
    for (WeatherDayModel *daymodel in model.daily) {
     
        UIView * dayview = [UIView new];
        [fourDayView addSubview:dayview];
        dayview.frame = CGRectMake(kwt(7), Y, kwt(345), kwt(46));
        [DTKTool YJBK:kwt(7) andView:dayview andWidth:1 andColor:KcolorHex(@"#F2F2F2")];
        NSString * day = @"";
        if([daymodel.fxDate isEqualToString: [NSDate br_stringFromDate:[NSDate new] dateFormat:@"yyyy-MM-dd"]]){
            day = [NSString stringWithFormat:@"今天 %lu/%02lu",[NSDate br_dateFromString:daymodel.fxDate dateFormat:@"yyyy-MM-dd"].br_month,[NSDate br_dateFromString:daymodel.fxDate dateFormat:@"yyyy-MM-dd"].br_day];
            dayview.backgroundColor = KcolorHex(@"#F2F8FF");
            [DTKTool YJBK:kwt(7) andView:dayview andWidth:1 andColor:KcolorHex(@"#D4DCDF")];
            [DTKTool YJYY:kwt(7) andView:dayview andWidth:1 andColor:KcolorHex(@"#84C7FF") andSize:CGSizeMake(0, 0) andOpacity:0.5];
        } else {
            day = [NSString stringWithFormat:@"%@ %lu/%02lu",[NSDate br_dateFromString:daymodel.fxDate dateFormat:@"yyyy-MM-dd"].br_weekdayString,[NSDate br_dateFromString:daymodel.fxDate dateFormat:@"yyyy-MM-dd"].br_month,[NSDate br_dateFromString:daymodel.fxDate dateFormat:@"yyyy-MM-dd"].br_day];
        }
        
        
        UILabel * temp41 = [DTKTool labelWithText:day andFont:kCFont(17) andTextColor:KcolorHex(@"#222222")];
        [dayview addSubview:temp41];
        [temp41 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.offset(0);
            make.left.offset(kwt(8));
            make.height.offset(kwt(17));
        }];

        UIImageView * tempimg = kimageView(daymodel.iconDay);
        tempimg.contentMode = UIViewContentModeScaleAspectFit;
        [dayview addSubview:tempimg];
        [tempimg mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.offset(0);
            make.left.offset(kwt(118));
            make.width.height.offset(kwt(30));
        }];
        UILabel * temp42 = [DTKTool labelWithText:daymodel.textDay andFont:kCFont(17) andTextColor:KcolorHex(@"#222222")];
        [dayview addSubview:temp42];
        [temp42 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.offset(0);
            make.left.offset(kwt(150));
        }];
        
        UILabel * temp43 = [DTKTool labelWithText:[NSString stringWithFormat:@"%@°",daymodel.tempMin] andFont:kCFont(17) andTextColor:KcolorHex(@"#222222")];
        [dayview addSubview:temp43];
        [temp43 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.offset(0);
            make.right.offset(kwt(-95));
            make.height.offset(kwt(17));
        }];
        UILabel * temp44 = [DTKTool labelWithText:[NSString stringWithFormat:@"%@°",daymodel.tempMax] andFont:kCFont(17) andTextColor:KcolorHex(@"#222222")];
        [dayview addSubview:temp44];
        [temp44 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.offset(0);
            make.left.offset(kwt(310));
            make.height.offset(kwt(17));
        }];

        UIView * bjview = [UIView new];
        bjview.backgroundColor = KcolorHex(@"#D2DBE6");
        [dayview addSubview:bjview];
        [DTKTool YJ:kwt(3.5) andView:bjview];
        [bjview mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.offset(0);
            make.left.offset(kwt(255));
            make.height.offset(kwt(7));
            make.width.offset(kwt(50));
        }];
        
        UIView * qjview = [UIView new];
        qjview.backgroundColor = [UIColor gradientColorWithSize:CGSizeMake(1, kwt(7)) direction:GradientColorDirectionVertical startColor:KcolorHex(@"#EAA300") endColor:KcolorHex(@"#F19B45")];
        [bjview addSubview:qjview];
        [DTKTool YJ:kwt(3.5) andView:qjview];
        [qjview mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.offset(0);
            make.left.offset(kwt((daymodel.tempMin.intValue - min)*(50.0/(max-min))));
            make.width.offset(kwt(50.0 *(daymodel.tempMax.intValue - daymodel.tempMin.intValue)/(max-min)));
            make.height.offset(kwt(7));
        }];
        
        
        Y += kwt(56);
        
    }
}


@end
